//
//  LLScreenView.m
//  TaoTuo
//
//  Created by LG on 2020/11/18.
//  Copyright © 2020 刘广. All rights reserved.
//  筛选view

#import "LLScreenView.h"
#import "LLScreenSectionModel.h"
#import "LLScreenItemsViewCell.h"
#import <UIView+TYAlertView.h>
#import "TTCustomHeaderView.h"

#import "TTCustomGoodsTextFieldViewCell.h"
#import "TTCertificateChooseViewCell.h"
#import "WSDatePickerView.h"
#import "TTDetailStockListModel.h"
#import "TTCustomGoodsConfirmViewController.h"
#import "TTCustomConditionModel.h"


@interface LLScreenView ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) TTCustomConditionModel *customModel;

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) TTCustomHeaderView *headerView;
/// 证书
@property (nonatomic, strong) TTCertificateChooseModel *zsModel;

@property (nonatomic, strong) UIView *footerView;

/// 获取定制商品列表的参数
@property (nonatomic, strong) NSMutableDictionary *paramsDic;
/// 金重、工费、损耗
@property (nonatomic, copy) NSString *firstRowString;
/// 加急费用描述
@property (nonatomic, copy) NSString *jjString;
/// 证书鉴定分级费用描述
@property (nonatomic, copy) NSString *zsString;

//@property (nonatomic, weak) UINavigationController *nav;


@end

@implementation LLScreenView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        UIBezierPath *bgPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(10, 10)];
        CAShapeLayer *bgLayer = [CAShapeLayer layer];
        bgLayer.path = bgPath.CGPath;
        bgLayer.fillColor = UIColor.whiteColor.CGColor;
        self.layer.mask = bgLayer;
        
        [self tableView];
        
        UIButton *closeBtn = [UIButton createButtonWithImage:[UIImage imageNamed:@"close_gray"] target:self action:@selector(dismiss)];
        closeBtn.contentEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
        [self addSubview:closeBtn];
        [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.top.equalTo(self);
        }];
        
    }
    return self;
}

- (void)showInView:(UIView *)view
{
    if (view == nil) {
        [LLAnimationAlertView animationAlertViewWithView:self animationType:AnimationFromBottom];
    } else {
        [LLAnimationAlertView animationAlertViewWithView:self animationType:AnimationFromBottom tapBgDismiss:YES inView:view];
    }
    
}

- (void)dismiss
{
    [LLAnimationAlertView dismissView:self];
}

- (void)setParamsDicWithKey:(NSString *)key value:(nullable NSString *)value
{
    if (key.length == 0) {
        return;
    }
    if (value.length == 0) {
        [self.paramsDic removeObjectForKey:key];
        return;
    }
    
    [self.paramsDic setObject:value forKey:key];
    
    /// 获取定制列表
    [self getCustomGoodsListDatas];
}

- (void)setStyleLibraryId:(NSString *)styleLibraryId
{
    _styleLibraryId = styleLibraryId;
    [self getCustomConditionData];
}

/// 获取款式定制条件
- (void)getCustomConditionData
{
    NSDictionary *dic = @{
        @"param.styleLibraryId" : self.styleLibraryId
    };
    @weakify(self)
    [[LLNetworkManagerTool sharedManager] requestMethod:GET urlString:getCustomCondition_api parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        @strongify(self)
        self.customModel = [TTCustomConditionModel modelWithJSON:responseObject];
        self.customModel.CoverMapImg = self.CoverMapImg;
        [self setupDatas];
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {
        
        
    } showLoadingInView:nil];
}


- (void)setupDatas
{
    
    [self.paramsDic removeAllObjects];
    [self.dataArray removeAllObjects];
    
    self.firstRowString = @"";
    self.jjString = @"";
    self.zsString = @"";
    [self setupHeaderViewInfoString];
    
    // 镶口大小
    @weakify(self)
    if (_customModel.IsShowInsertSize) {
        NSMutableArray *xkDataArray = [NSMutableArray array];
        for (TTCustomConditionItemModel *model in _customModel.InsertSize) {
            LLScreenItemModel *itemModel = [LLScreenItemModel setModelWithTitle:model.Name value:model.Value isEnable:model.IsEnable];
            [xkDataArray addObject:itemModel];
        }
        LLScreenSectionModel *xkModel = [LLScreenSectionModel setModelWithSectionTitle:@"镶口大小" allowMulti:NO allowCancelSelect:YES dataArray:xkDataArray];
        xkModel.selectedItemBlock = ^(NSArray<LLScreenItemModel *> * _Nonnull selectedItemArray) {
            @strongify(self)
            LLScreenItemModel *m = selectedItemArray.firstObject;
            [self setParamsDicWithKey:@"param.insertSize" value:m.value];
            
        };
        
        [self.dataArray addObject:xkModel];
    }
    
    
    // 手寸大小
    if (_customModel.IsShowHandSize) {
        NSMutableArray *scDataArray = [NSMutableArray array];
        for (TTCustomConditionItemModel *model in _customModel.HandSize) {
            LLScreenItemModel *itemModel = [LLScreenItemModel setModelWithTitle:model.Name value:model.Value isEnable:model.IsEnable];
            [scDataArray addObject:itemModel];
        }
        LLScreenSectionModel *scModel = [LLScreenSectionModel setModelWithSectionTitle:@"手寸大小" allowMulti:NO allowCancelSelect:YES dataArray:scDataArray];
        scModel.selectedItemBlock = ^(NSArray<LLScreenItemModel *> * _Nonnull selectedItemArray) {
            @strongify(self)
            LLScreenItemModel *m = selectedItemArray.firstObject;
            [self setParamsDicWithKey:@"param.handSize" value:m.value];
        };
        
        [self.dataArray addObject:scModel];
    }
    
    // 手镯内径
    if (_customModel.IsShowBraceletSize) {
        NSMutableArray *szDataArray = [NSMutableArray array];
        for (TTCustomConditionItemModel *model in _customModel.BraceletSize) {
            LLScreenItemModel *itemModel = [LLScreenItemModel setModelWithTitle:model.Name value:model.Value isEnable:model.IsEnable];
            [szDataArray addObject:itemModel];
        }
        LLScreenSectionModel *szModel = [LLScreenSectionModel setModelWithSectionTitle:@"手镯内径" allowMulti:NO allowCancelSelect:YES dataArray:szDataArray];
        szModel.selectedItemBlock = ^(NSArray<LLScreenItemModel *> * _Nonnull selectedItemArray) {
            @strongify(self)
            LLScreenItemModel *m = selectedItemArray.firstObject;
            [self setParamsDicWithKey:@"param.braceletSize" value:m.value];
        };
        
        [self.dataArray addObject:szModel];
    }
    
    // 成色
    NSMutableArray *colorDataArray = [NSMutableArray array];
    for (TTCustomConditionItemModel *model in _customModel.GoldColor) {
        LLScreenItemModel *itemModel = [LLScreenItemModel setModelWithTitle:model.Name value:model.Value isEnable:model.IsEnable];
        [colorDataArray addObject:itemModel];
    }
    if (colorDataArray.count > 0) {
        LLScreenSectionModel *colorModel = [LLScreenSectionModel setModelWithSectionTitle:@"金成色" allowMulti:NO allowCancelSelect:YES dataArray:colorDataArray];
        colorModel.selectedItemBlock = ^(NSArray<LLScreenItemModel *> * _Nonnull selectedItemArray) {
            @strongify(self)
            LLScreenItemModel *m = selectedItemArray.firstObject;
            [self setParamsDicWithKey:@"param.goldColor" value:m.value];
        };
        
        [self.dataArray addObject:colorModel];
    }
    
    // 产品做工需求
    NSMutableArray *zgArray = [NSMutableArray array];
    for (TTCustomConditionItemModel *model in _customModel.Grade) {
        LLScreenItemModel *itemModel = [LLScreenItemModel setModelWithTitle:model.Name value:model.Value isEnable:model.IsEnable];
        [zgArray addObject:itemModel];
    }
    if (zgArray.count > 0) {
        LLScreenSectionModel *zgModel = [LLScreenSectionModel setModelWithSectionTitle:@"产品做工需求" allowMulti:YES allowCancelSelect:NO dataArray:zgArray];
        zgModel.selectedItemBlock = ^(NSArray<LLScreenItemModel *> * _Nonnull selectedItemArray) {
            @strongify(self)
            LLScreenItemModel *m = selectedItemArray.firstObject;
            [self setParamsDicWithKey:@"param.goodsGrade" value:m.value];
        };
        
        [self.dataArray addObject:zgModel];
    }
    
    // 表面工艺
    NSMutableArray *gyArray = [NSMutableArray array];
    for (NSString *str in _customModel.Technology) {
        LLScreenItemModel *itemModel = [LLScreenItemModel setModelWithTitle:str value:str isEnable:YES];
        [gyArray addObject:itemModel];
    }
    if (gyArray.count > 0) {
        LLScreenSectionModel *gyModel = [LLScreenSectionModel setModelWithSectionTitle:@"表面工艺" allowMulti:NO allowCancelSelect:YES dataArray:gyArray];
        gyModel.selectedItemBlock = ^(NSArray<LLScreenItemModel *> * _Nonnull selectedItemArray) {
//            @strongify(self)
//            LLScreenItemModel *m = selectedItemArray.firstObject;
//            [self setParamsDicWithKey:@"param.goodsGrade" value:m.value];
        };
        
        [self.dataArray addObject:gyModel];
    }
    
    // 刻字
    NSString *infoStr = @"友情提示：指圈较小的托，字太长会导致刻不下";
    TTCustomGoodsTFModel *textModel = [TTCustomGoodsTFModel setModelWithTitle:@"刻字" content:@"" infoStr:infoStr];
    [self.dataArray addObject:textModel];
    
    
    // 刻字字体
    NSMutableArray *ztDataArray = [NSMutableArray array];
    for (TTCustomConditionItemModel *model in _customModel.LetteringFont) {
        LLScreenItemModel *itemModel = [LLScreenItemModel setModelWithTitle:model.Name value:model.Value isEnable:YES];
        itemModel.rightIcon = model.Icon;
        [ztDataArray addObject:itemModel];
    }
    if (ztDataArray.count > 0) {
        LLScreenSectionModel *ztModel = [LLScreenSectionModel setModelWithSectionTitle:@"刻字字体" allowMulti:NO allowCancelSelect:YES dataArray:ztDataArray];
        @weakify(ztModel)
        ztModel.selectedItemBlock = ^(NSArray<LLScreenItemModel *> * _Nonnull selectedItemArray) {
            @strongify(ztModel)
            LLScreenItemModel *model = selectedItemArray.firstObject;
            ztModel.rightImageName = model.rightIcon;
            [self.tableView reloadRow:[self.dataArray indexOfObject:ztModel] inSection:0 withRowAnimation:UITableViewRowAnimationNone];
        };
        [self.dataArray addObject:ztModel];
    }
    
    // 厂印
    NSMutableArray *cyDataArray = [NSMutableArray array];
    for (TTCustomConditionItemModel *model in _customModel.FactoryPrint) {
        LLScreenItemModel *itemModel = [LLScreenItemModel setModelWithValue:model.Name unitString:@"" imageName:model.Value];
        itemModel.isEnable = YES;
        [cyDataArray addObject:itemModel];
    }
    LLScreenSectionModel *cyModel = [LLScreenSectionModel setModelWithSectionTitle:@"厂印" allowMulti:NO allowCancelSelect:YES dataArray:cyDataArray];
    cyModel.hintString = @"友情提示：选择厂印需要提供品牌授权文件，详询您的业务员。";
    cyModel.hintImageName = @"hintgantanhao";
    cyModel.hintTextColor = UIColorHex(#FC832A);
    cyModel.minSize = CGSizeMake(100, 30);
    cyModel.selectedItemBlock = ^(NSArray<LLScreenItemModel *> * _Nonnull selectedItemArray) {
        for (LLScreenItemModel *m in selectedItemArray) {
            NSLog(@">>> %@, %@", m.title, m.imageName);
        }
    };
    [self.dataArray addObject:cyModel];
    
    // 证书
    NSMutableArray *zsDataArray = [NSMutableArray array];
    for (TTCustomConditionCertModel *certModel in _customModel.Cert) {
        TTCertificateItemModel *model = [TTCertificateItemModel setModelWithTitle:certModel.Name infoStr:certModel.FullName];
        [zsDataArray addObject:model];
        NSMutableArray *certBtns = [NSMutableArray array];
        for (TTCustomConditionChildCertModel *cm in certModel.ChildCert) {
            NSString *str = [NSString stringWithFormat:@"%@¥%@", cm.Name, cm.Price];
            [certBtns addObject:str];
        }
        model.btnTitlesArray = certBtns;
    }
    if (zsDataArray.count > 0) {
        self.zsModel = [TTCertificateChooseModel setModelWithSectionTitle:@"证书" allowMulti:NO allowCancelSelect:YES dataArray:zsDataArray];
        
        @weakify(self)
        self.zsModel.selectedItemBlock = ^(NSArray<TTCertificateItemModel *> * _Nonnull selectedItemArray) {
            @strongify(self)
            if (selectedItemArray.count > 0) {
                TTCertificateItemModel *itemModel = selectedItemArray.firstObject;
                self.zsModel.btnTitlesArray = itemModel.btnTitlesArray;
            } else {
                self.zsModel.btnTitlesArray = @[];
            }
            
            [self.tableView reloadRow:[self.dataArray indexOfObject:self.zsModel] inSection:0 withRowAnimation:UITableViewRowAnimationNone];
        };
        self.zsModel.selectedBtnBlock = ^(NSString * _Nonnull title, NSInteger index) {
            @strongify(self)
            self.zsString = @"";
            for (NSString *title in self.zsModel.selectedBtnTitleArray) {
                self.zsString = [NSString stringWithFormat:@"%@%@  ", self.zsString, title];
            }
            
            [self setupHeaderViewInfoString];
            
        };
        
        [self.dataArray addObject:self.zsModel];
    }
    
    // 期望收货时间
    TTCustomGoodsTFModel *timeModel = [TTCustomGoodsTFModel setModelWithTitle:@"期望收货时间" content:@"" infoStr:nil];
    timeModel.notEdit = YES;
    timeModel.aboveBtnBlock = ^{
        @strongify(self)
        [self chooseDateWithModel:timeModel];
    };
    [self.dataArray addObject:timeModel];
    
    // 加急
    LLScreenItemModel *jjItemModel = [LLScreenItemModel setModelWithValue:@"加急（收费:¥300）" unitString:nil btnImageName:@"tag_shandian"];
    jjItemModel.isEnable = YES;
    LLScreenSectionModel *jjModel = [LLScreenSectionModel setModelWithSectionTitle:@"加急（工期3-7天）" allowMulti:YES allowCancelSelect:YES dataArray:@[jjItemModel]];
    jjModel.hintTextColor = UIColorHex(#F7172B);
    jjModel.minSize = CGSizeMake(160, 30);
    @weakify(jjModel)
    jjModel.selectedItemBlock = ^(NSArray<LLScreenItemModel *> * _Nonnull selectedItemArray) {
        @strongify(jjModel)
        @strongify(self)
        if (selectedItemArray.count > 0) {
            jjModel.hintString = @"我们将为您安排加急服务，预计15-18号可以发货";
        } else {
            jjModel.hintString = @"";
        }
        self.jjString = selectedItemArray.count > 0 ? @"加急:300元" : @"";
        
        [self setupHeaderViewInfoString];
        
        [self.tableView reloadRow:[self.dataArray indexOfObject:jjModel] inSection:0 withRowAnimation:UITableViewRowAnimationNone];
    };
    [self.dataArray addObject:jjModel];
    
    
    
    self.headerView.customModel = _customModel;
    self.tableView.contentInset = UIEdgeInsetsMake(self.headerView.height-40, 0, 0, 0);
    
    
    self.tableView.tableFooterView = self.footerView;
    
    [self.tableView reloadData];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSObject *obj = self.dataArray[indexPath.row];
    if ([obj isKindOfClass:[LLScreenSectionModel class]]) {
        LLScreenSectionModel *model = (LLScreenSectionModel *)obj;
        LLScreenItemsViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LLScreenItemsViewCell class])];
        cell.model = model;
        return cell;
    } else if ([obj isKindOfClass:[TTCertificateChooseModel class]]) {
        TTCertificateChooseModel *model = (TTCertificateChooseModel *)obj;
        TTCertificateChooseViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([TTCertificateChooseViewCell class])];
        cell.model = model;
        return cell;
    } else {
        TTCustomGoodsTFModel *model = (TTCustomGoodsTFModel *)obj;
        TTCustomGoodsTextFieldViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([TTCustomGoodsTextFieldViewCell class])];
        cell.model = model;
        return cell;
    }
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSObject *obj = self.dataArray[indexPath.row];
    if ([obj isKindOfClass:[LLScreenSectionModel class]]) {
        LLScreenSectionModel *model = (LLScreenSectionModel *)obj;
        return model.hintString.length > 0 ? 95 : 80;
    } else if ([obj isKindOfClass:[TTCertificateChooseModel class]]) {
        return self.zsModel.btnTitlesArray.count > 0 ? 170 : 140;
    } else {
        TTCustomGoodsTFModel *model = (TTCustomGoodsTFModel *)obj;
        return model.infoStr.length > 0 ? 100 : 80;
    }
}

- (void)chooseDateWithModel:(TTCustomGoodsTFModel *)model
{
    [self endEditing:YES];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyy-MM-dd"];
    NSString *nowDate = [formatter stringFromDate:[NSDate date]];
    
    NSDate *afterDate = [[NSDate date] dateByAddingDays:25];
    
    @weakify(self)
    WSDatePickerView *datepicker = [[WSDatePickerView alloc] initWithDateStyle:DateStyleShowYearMonthDay minLimitDateString:nowDate maxLimitDateString:@"2100-1-1" scrollToDate:afterDate CompleteBlock:^(NSDate *selectDate) {
        @strongify(self)
        NSString *date = [selectDate stringWithFormat:@"yyyy-MM-dd"];
        model.content = date;
        [self.tableView reloadRow:[self.dataArray indexOfObject:model] inSection:0 withRowAnimation:UITableViewRowAnimationNone];
    }];
    datepicker.doneButtonColor = kMainColor;
    datepicker.dateLabelColor = kMainColor;
    datepicker.hideBackgroundYearLabel = YES;
    [datepicker show];
}

/// 获取定制商品列表
- (void)getCustomGoodsListDatas
{
    [self.paramsDic setObject:@(1) forKey:@"param.pageIndex"];
    [self.paramsDic setObject:@(10) forKey:@"param.pageSize"];
    [self.paramsDic setObject:self.styleLibraryId forKey:@"param.styleLibraryId"];
    
    if (![self.paramsDic.allKeys containsObject:@"param.goodsGrade"]) {
        return;
    }
    [[LLNetworkManagerTool sharedManager] postUrlString:getCustomList_api parameters:self.paramsDic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSArray *arr = responseObject;
        if (arr.count == 1) {        
            TTDetailStockListModel *model = [TTDetailStockListModel modelWithJSON:arr.firstObject];
            self.firstRowString = [NSString stringWithFormat:@"金重:%@  工费:%@  损耗:%@", model.PureWeight, model.WorkTotalPrice, model.GoldLoss];
            self.headerView.price = model.Price;
        } else {
            self.firstRowString = @"";
            self.headerView.price = @"";
        }
        [self setupHeaderViewInfoString];
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {
        
        
    } showLoadingInView:nil];
}

- (void)setupHeaderViewInfoString
{
    NSString *str1 = @"";
    if (self.firstRowString.length > 0) {
        str1 = [NSString stringWithFormat:@"%@\n", self.firstRowString];
    }
    NSString *str2 = @"";
    if (self.jjString.length > 0) {
        str2 = [NSString stringWithFormat:@"%@\n", self.jjString];
    }
    
    self.zsString = self.zsString ? : @"";
    self.headerView.infoString = [NSString stringWithFormat:@"%@%@%@", str1, str2, self.zsString];
}

/// 立即购买
- (void)footerBtnClick
{
//    [self dismiss];
    
    NSMutableArray *array = [NSMutableArray array];
    for (id obj in self.dataArray) {
        if ([obj isKindOfClass:[LLScreenSectionModel class]]) {
            LLScreenSectionModel *sectionModel = (LLScreenSectionModel *)obj;
            for (LLScreenItemModel *model in sectionModel.dataArray) {
                if (model.isSelected) {
                    if ([sectionModel.sectionTitle hasPrefix:@"加急"]) {
                        [array addObject:@{@"加急" : @"已加急"}];
                    } else {
                        [array addObject:@{sectionModel.sectionTitle : model.title}];
                    }
                    break;
                }
            }
            
        } else if ([obj isKindOfClass:[TTCustomGoodsTFModel class]]) {
            TTCustomGoodsTFModel *model = (TTCustomGoodsTFModel *)obj;
            if (model.content.length > 0) {
                [array addObject:@{model.title : model.content}];
            }
        } else if ([obj isKindOfClass:[TTCertificateChooseModel class]]) {
            TTCertificateChooseModel *model = (TTCertificateChooseModel *)obj;
            for (LLScreenItemModel *itemModel in model.dataArray) {
                if (itemModel.isSelected) {
                    [array addObject:@{@"检测单位" : itemModel.title}];
                    break;
                }
            }
            if (self.zsString.length > 0) {
                [array addObject:@{model.sectionTitle : self.zsString}];
            }
        }
    }
    
    TTCustomGoodsConfirmViewController *confirmVC = [[TTCustomGoodsConfirmViewController alloc] init];
    [confirmVC setHeaderViewWithPrice:self.headerView.price info:self.headerView.infoString customModel:self.customModel];
    confirmVC.array = array;
    [[LLRouteTool getCurrentNavigationController] pushViewController:confirmVC animated:YES];
    
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[LLScreenItemsViewCell class] forCellReuseIdentifier:NSStringFromClass([LLScreenItemsViewCell class])];
        [_tableView registerClass:[TTCustomGoodsTextFieldViewCell class] forCellReuseIdentifier:NSStringFromClass([TTCustomGoodsTextFieldViewCell class])];
        [_tableView registerClass:[TTCertificateChooseViewCell class] forCellReuseIdentifier:NSStringFromClass([TTCertificateChooseViewCell class])];
        [self addSubview:_tableView];
    }
    return _tableView;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (NSMutableDictionary *)paramsDic
{
    if (!_paramsDic) {
        _paramsDic = [NSMutableDictionary dictionary];
    }
    return _paramsDic;
}

- (TTCustomHeaderView *)headerView
{
    if (!_headerView) {
        _headerView = [[TTCustomHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.width, 130)];
        _headerView.backgroundColor = [UIColor whiteColor];
        [self insertSubview:_headerView aboveSubview:self.tableView];
    }
    return _headerView;
}


- (UIView *)footerView
{
    if (!_footerView) {
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, 70)];
        _footerView.backgroundColor = UIColor.whiteColor;
        
        UIButton *footerBtn = [UIButton createButtonWithTitle:@"立即购买" titleColor:UIColor.whiteColor titleFont:[UIFont boldSystemFontOfSize:16] backgroundColor:UIColorHex(#8B57E6) cornerRadius:20 target:self action:@selector(footerBtnClick)];
        footerBtn.frame = CGRectMake(15, 20, self.width - 30, 40);
        [_footerView addSubview:footerBtn];
    }
    return _footerView;
}

@end
